package com.whd.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whd.demo.common.Result;
import com.whd.demo.entity.House;
import com.whd.demo.mapper.HouseMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Resource
    HouseMapper houseMapper;

    /**
     * 新增房子
     * @param house
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody House house) {
        houseMapper.insert(house);
        return Result.success();
    }

    /**
     * 分页查询（查询所有房屋的信息，包含房主姓名）（管理员专属功能）
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(House::getHouseAddress,search);
        }
        Page<House> housePage = houseMapper.findPage(new Page<>(pageNum,pageSize),search);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询自己上传的房屋信息）（房主专属功能）
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/ownerHouse")
    public Result<?> findPageOwner(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                                   @RequestParam int ownerId){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery().eq(House::getOwnerId,ownerId);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(House::getHouseAddress,search);
        }
        Page<House> housePage = houseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询所有可供出租的房屋）（租客专属功能）
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/rentableHouse")
    public Result<?> findPageOwner(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery().eq(House::getIsAble,1);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(House::getHouseAddress,search);
        }
        Page<House> housePage = houseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询自己已经租到的房屋）（租客专属功能）
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/alreadyRent")
    public Result<?> findRent(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   @RequestParam(defaultValue = "") String search,
                                   @RequestParam Integer tenantId){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery().eq(House::getTenantId,tenantId);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(House::getHouseAddress,search);
        }
        Page<House> housePage = houseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询自己已经租出的房屋）（房主专属功能）
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/alreadyRented")
    public Result<?> findRented(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam Integer ownerId){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery().eq(House::getOwnerId,ownerId).ne(House::getTenantId,-1);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(House::getHouseAddress,search);
        }
        Page<House> housePage = houseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询自己还没有申请的房屋）（租客专属功能）
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/findNotRent")
    public Result<?> findNotRentHouse(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam Integer id,
                                       @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(House::getHouseAddress, search);
        }
        Page<House> housePage = houseMapper.findNotRent(new Page<>(pageNum,pageSize),id,search);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询自己的意向房屋）（租客专属功能）
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @GetMapping("/findWantRent")
    public Result<?> findWantRentHouse(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                       @RequestParam Integer id,
                                       @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(House::getHouseAddress, search);
        }
        Page<House> housePage = houseMapper.findRent(new Page<>(pageNum,pageSize),id,search);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询某房主的房屋信息）（管理员专属功能）
     * @param pageNum
     * @param pageSize
     * @param ownerId
     * @return
     */
    @GetMapping("/selectHouse")
    public Result<?> selectHouseByOwnerId(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   @RequestParam Integer ownerId){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery().eq(House::getOwnerId,ownerId);
        Page<House> housePage = houseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(housePage);
    }

    /**
     * 分页查询（查询房屋的信息）（会面信息模块的功能）
     * @param pageNum
     * @param pageSize
     * @param houseId
     * @return
     */
    @GetMapping("/selectHouseById")
    public Result<?> selectHouseById(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "5") Integer pageSize,
                                          @RequestParam Integer houseId){
        LambdaQueryWrapper<House> wrapper = Wrappers.<House>lambdaQuery().eq(House::getId,houseId);
        Page<House> housePage = houseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(housePage);
    }

    /**
     * 根据房子id查询房子信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(houseMapper.selectById(id));
    }


    /**
     * 删除房屋
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        houseMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 更新房屋信息
     * @param house
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody House house){
        houseMapper.updateById(house);
        return Result.success();
    }

    /**
     * 租客确认租房
     * @param houseId
     * @param tenantId
     * @return
     */
    @PutMapping("/confirm/{houseId}/{tenantId}")
    public Result<?> confirmRent(@PathVariable Integer houseId,@PathVariable Integer tenantId){
        House house = houseMapper.selectById(houseId);
        house.setTenantId(tenantId);
        houseMapper.updateById(house);
        return Result.success();
    }

    /**
     * 租客退租
     * @param houseId
     * @return
     */
    @PutMapping("/deleteRent/{houseId}")
    public Result<?> deleteRent(@PathVariable Integer houseId){
        House house = houseMapper.selectById(houseId);
        house.setTenantId(-1);
        houseMapper.updateById(house);
        return Result.success();
    }

}
